package cinema.dao;


import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Set;

import org.hibernate.Session;

import cinema.model.Actors;
import cinema.model.Genres;
import cinema.model.Review;
import cinema.model.Tickets;
import cinema.model.Users;
import cinema.utils.HibernateUtil;

public class UsersDaoHibernate extends Initialization implements UsersDao {

	
	
	@Override
	public Users getUsersbyID(String ID) {
		// TODO Auto-generated method stub
		
		try{
			
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			
			session.beginTransaction();
			
	        List<Users> result = session.createQuery("from Users where ID = ?").setString(0, ID).list();
	        
	        
	        
	        if (result.size() > 0) {
	            
	        	ListIterator<Users> it = result.listIterator();
	            Users act = (Users) it.next();
	            

	            Iterator<Tickets> iter = act.getUserTickets().iterator();
	            
				Set<Tickets> tickets = new HashSet<Tickets>();
				
				while(iter.hasNext()){
					Tickets tick = iter.next();
					tickets.add(tick);
				}
				
				act.setUserTickets(tickets);
				

				Iterator<Actors> iter2 = act.getActors().iterator();
	            
				Set<Actors> actors = new HashSet<Actors>();
				
				while(iter2.hasNext()){
					
					Actors actor = iter2.next();
					actors.add(actor);
					
				}
				
				act.setActors(actors);
				

				Iterator<Genres> iter3 = act.getGenres().iterator();
	            
				Set<Genres> genres = new HashSet<Genres>();
				
				while(iter3.hasNext()){
					
					Genres genre = iter3.next();
					genres.add(genre);
					
				}
				
				act.setGenres(genres);
				

				Iterator<Review> iter4 = act.getReviews().iterator();
	            
				Set<Review> reviews = new HashSet<Review>();
				
				while(iter4.hasNext()){
					
					Review review = iter4.next();
					reviews.add(review);
					
				}
				
				act.setReviews(reviews);
				
				act.setUserType(act.getUserType());
				
				
				
	            if (! HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().wasCommitted())
		        	session.getTransaction().commit();
	            
	            return act;
	        }
	        else{
	        	
	        	if (! HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().wasCommitted())
		        	session.getTransaction().commit();
	        	
	        	return null;
	        }
	        	
			
		}
		catch(Exception ex){
			return null;
		}
		
        
        
        
		
	}

	@Override
	public ListIterator<Users> getUsers() {
		// TODO Auto-generated method stub
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		
			session.beginTransaction();
								
			List<Users> result = session.createCriteria(Users.class).list();
			
			for(Users act : result){
	            

	            Iterator<Tickets> iter = act.getUserTickets().iterator();
	            
				Set<Tickets> tickets = new HashSet<Tickets>();
				
				while(iter.hasNext()){
					Tickets tick = iter.next();
					tickets.add(tick);
				}
				
				act.setUserTickets(tickets);
				

				Iterator<Actors> iter2 = act.getActors().iterator();
	            
				Set<Actors> actors = new HashSet<Actors>();
				
				while(iter2.hasNext()){
					
					Actors actor = iter2.next();
					actors.add(actor);
					
				}
				
				act.setActors(actors);
				

				Iterator<Genres> iter3 = act.getGenres().iterator();
	            
				Set<Genres> genres = new HashSet<Genres>();
				
				while(iter3.hasNext()){
					
					Genres genre = iter3.next();
					genres.add(genre);
					
				}
				
				act.setGenres(genres);
				

				Iterator<Review> iter4 = act.getReviews().iterator();
	            
				Set<Review> reviews = new HashSet<Review>();
				
				while(iter4.hasNext()){
					
					Review review = iter4.next();
					reviews.add(review);
					
				}
				
				act.setReviews(reviews);
				
				act.setUserType(act.getUserType());
				
			}
        
			ListIterator<Users> it = result.listIterator();
        
			if (! HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().wasCommitted())
	        	session.getTransaction().commit();
			
			return it;
			
		}
		catch(Exception ex){
			
			return null;
			
		}
	}
	

	@Override
	public boolean saveUsers(Users UsersObject) {
		// TODO Auto-generated method stub
		
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		
			session.beginTransaction();
			
			session.saveOrUpdate(UsersObject);
			
			if (! HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().wasCommitted())
	        	session.getTransaction().commit();
			
			return true;
		} catch (Exception e){
			return false;
		}
		
	}

	@Override
	public boolean deleteUsers(Users UsersObject) {
		// TODO Auto-generated method stub
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		
			session.beginTransaction();
			
			session.delete(UsersObject);
			
			if (! HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().wasCommitted())
	        	session.getTransaction().commit();
			
			return true;
		} catch (Exception e){
			return false;
		}
	}

}
